package com.zzn.bookManage.controller;

import com.zzn.bookManage.pojo.Book;
import com.zzn.bookManage.pojo.User;
import com.zzn.bookManage.service.BookService;
import com.zzn.bookManage.util.ObjectHolder;
import com.zzn.bookManage.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 新建图书信息
 */

@WebServlet("/newBook")
public class NewBook extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(true); // 必定获得一个session

        String name =req.getParameter("name");
        String author =req.getParameter("author");
        String publisher =req.getParameter("publisher");
        String description =req.getParameter("description");
        String position =req.getParameter("position");
        double price = Double.parseDouble(req.getParameter("price"));

        BookService bookService = ObjectHolder.getInstance().getBookService();
        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setPublisher(publisher);
        book.setDescription(description);
        book.setPosition(position);
        book.setPrice(price);

        if (bookService.addBook(book)){
            // 新建图书信息成功
            resp.sendRedirect("/list?page=1&msg="+java.net.URLEncoder.encode("新建图书信息成功","UTF-8"));
        }else{
            // 新建图书信息失败
            resp.sendRedirect("/list?page=1&msg="+java.net.URLEncoder.encode("新建图书信息失败","UTF-8"));
        }
    }
}
